package tr.edu.ege.kipler.syntax;

import tr.edu.ege.kipler.common.Copyable;

/**
 * 
 * @author İnanç Seylan
 *
 */
public class Role implements Copyable<Role> {
	private Name name;

	private boolean isInverse;

	public Role() {

	}

	public Role(String name) {
		setName(name);
	}
	
	public Role(String name, boolean inverse) {
		setName(name);
		setInverse(inverse);
	}
	
	public Role toggleInverse() {
		setInverse(!isInverse());
		return this;
	}

	/**
	 * @return the isInverse
	 */
	public boolean isInverse() {
		return isInverse;
	}

	/**
	 * @param isInverse
	 *            the isInverse to set
	 */
	public void setInverse(boolean isInverse) {
		this.isInverse = isInverse;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name.toString();
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = NameManager.getInstance().getName(name);
	}

	public Role copy() {
		Role result = new Role();
		result.name = this.name;
		result.isInverse = this.isInverse;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj != null && (obj.getClass().equals(this.getClass()))) {
			Role other = (Role) obj;
			return this.name.equals(other.name)
					&& this.isInverse() == other.isInverse();
		}
		return false;
	}

	@Override
	public int hashCode() {
		return 17 * 37 + name.hashCode() + new Boolean(isInverse).hashCode();
	}

	@Override
	public String toString() {
		String name = NameManager.getInstance().toQname(getName().toString());
		return (isInverse() ? name + "-" : name);
	}

}
